package com.yidu.hbatsft.action;



import com.yidu.hbatsft.service.impl.AtsRivalWordServiceImpl;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.vo.UserVo;
import com.yidu.utils.Config;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;


import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet("/ExportServlet")
public class ExportServlet extends HttpServlet {


    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    Config config;

    @Resource
    AtsRivalWordServiceImpl atsRivalWordService;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserVo managerModel = (UserVo) request.getSession().getAttribute("manager");

        if(managerModel==null){
            request.getRequestDispatcher("index.html").forward(request,response);
            return ;
        }

        String action = request.getParameter("action");
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        OutputStream os = response.getOutputStream();


        String contextPath = config.getPath();
        //managerModel.setConpanyNameXg(contextPath);//为了节约一个参数，使用了managerModel里面的一个属性存值，将本机上下文存在里面，方便后面下载文件使用的地址
        XWPFDocument document = null ;
        if("rival".equals(action)){
            response.setContentType("application/vnd.ms-word");
            document = atsRivalWordService.createWord(Integer.valueOf(id),response,managerModel);
        }

        document.write(os);

        document.close();

        os.flush();

        os.close();

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
